//
//  Date+Method.swift
//  BaseComponent
//
//  Created by ryan on 2021/6/4.
//

import Foundation


public enum CDateFormatStyle: String {
    case yyyy                      = "yyyy"
    case yyyy_MM                   = "yyyy-MM"
    case yyyy_MM_DD                 = "yyyy-MM-dd"
    case yyyy_MM_DD_HH_mm_ss        = "yyyy-MM-dd HH:mm:ss"
    case yyyy_MM_DD_HH_mm_ss_SSSS   = "yyyy-MM-dd HH:mm:ss.SSSS"
    case HH_mm_ss                   = "HH:mm:ss"
}

public extension Date {
    //MARK:- 计算属性
    
    static var getTimestamp: Int {
        //10位数时间戳
        return Int(Date().timeIntervalSince1970)
    }
    

    //MARK:- 类方法
    
    static func getDateForTimeStamp(timeStamp: Int) -> Date {
        return Date(timeIntervalSince1970: TimeInterval(timeStamp))
    }
    
    /// 获取最近日期范围
    /// - Parameters:
    ///   - year: +- 年
    ///   - month: +- 月
    ///   - day: +- 天
    ///   - separator: 分割符号
    /// - Returns: 日期区间字符串
    static func getRecentlyDateStr(year: Int = 0, month: Int = 0, day: Int, separator: String = "~") -> String {
        let currentDate = Date()
    
        let calendar = Calendar(identifier: .gregorian)
        var dateComps = DateComponents()
        dateComps.year = year
        dateComps.month = month
        dateComps.day = day

        let calulateDate = calendar.date(byAdding: dateComps, to: currentDate)
        let formatter = DateFormatter()
        formatter.dateFormat = CDateFormatStyle.yyyy_MM_DD.rawValue
        let calculateStr = formatter.string(from: calulateDate!)
        let dateStr = formatter.string(from: currentDate)
        
        print(dateStr, calculateStr)
        return calculateStr + separator + dateStr
    }
    
}
